package com.neusoft;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.neusoft.elm.po.Business;

/**
 * Servlet implementation class Servlet03
 */
@WebServlet("/Servlet03")
public class Servlet03 extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Servlet03() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//设置允许跨域访问,"*":允许所有url跨域访问
		//设置允许跨域访问，并且设置允许什么样的url进行跨域访问。
        response.setHeader("Access-Control-Allow-Origin", "*"); 
		
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        
        
		Business business = new Business();
		business.setBusinessName("eatrioko");
		business.setId(1);
		
		//ObjectMapper类是jackson的主要类
		ObjectMapper om = new ObjectMapper();
		//writeValueAsString方法可以把【类对象】输出成【json字符串】
		String businessStr = om.writeValueAsString(business);
		System.out.println(businessStr);
		
		PrintWriter out = response.getWriter();
		//页面输出
		out.print(businessStr);
		out.close();
		
		
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
